/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package myfilechooser;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import javax.swing.JFileChooser;

/**
 *
 * @author sochy
 */
public class MyViewImage extends javax.swing.JFrame {

    /**
     * Creates new form MyViewImage
     */
    private String path = null;
    String s = null;

    public MyViewImage() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnbrows = new javax.swing.JButton();
        imagepreview = new javax.swing.JLabel();
        btnpreview = new javax.swing.JButton();
        btnsave = new javax.swing.JButton();
        imagesaved = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        btnbrows.setText("Brows Image");
        btnbrows.setBorder(new javax.swing.border.MatteBorder(null));
        btnbrows.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnbrowsActionPerformed(evt);
            }
        });

        imagepreview.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        btnpreview.setText("Priview");
        btnpreview.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnpreviewActionPerformed(evt);
            }
        });

        btnsave.setText("Save");
        btnsave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnsaveActionPerformed(evt);
            }
        });

        imagesaved.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(imagepreview, javax.swing.GroupLayout.PREFERRED_SIZE, 193, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(33, 33, 33)
                        .addComponent(imagesaved, javax.swing.GroupLayout.PREFERRED_SIZE, 230, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnbrows)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnpreview)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnsave)))
                .addContainerGap(176, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnbrows)
                    .addComponent(btnpreview)
                    .addComponent(btnsave))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(imagepreview, javax.swing.GroupLayout.DEFAULT_SIZE, 183, Short.MAX_VALUE)
                    .addComponent(imagesaved, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(110, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnbrowsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnbrowsActionPerformed
        // TODO add your handling code here:
        JFileChooser fc = new JFileChooser();
        int f = fc.showOpenDialog(this);
        System.out.println(f);
        if (f == JFileChooser.APPROVE_OPTION) {

            File file = fc.getSelectedFile();

            String path = file.getPath();

            this.path = path;
            System.out.println(path);
        } else if (f == JFileChooser.CANCEL_OPTION) {
            System.out.println("File is not selected");
        }


    }//GEN-LAST:event_btnbrowsActionPerformed

    private void btnpreviewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnpreviewActionPerformed
        // TODO add your handling code here:
        // Get width and height form label
        int w = imagepreview.getWidth(); //My Button width
        int h = imagepreview.getHeight();//My button height
//
//        // get imageIcon from path
//        ImageIcon ii = new ImageIcon(this.path);
//        // get Image object from ImageIcon object
//        Image i = ii.getImage();
//        // Scale or set image size
//        Image img = i.getScaledInstance(w, h, java.awt.Image.SCALE_SMOOTH);
//        // Create icon object
//        Icon icon = new ImageIcon(img);
        //Icon icon = new ImageIcon("C:\\Users\\sochy\\Desktop\\logo.gif");
        
        imagepreview.setIcon(MagicImage.image(this.path, w, h));
    }//GEN-LAST:event_btnpreviewActionPerformed

    private void btnsaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnsaveActionPerformed

        File from = new File(this.path);
        //File to = new File

    }//GEN-LAST:event_btnsaveActionPerformed

    public static void copyFile(File from, File to) throws IOException {
        Files.copy(from.toPath(), to.toPath());
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(MyViewImage.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MyViewImage.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MyViewImage.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MyViewImage.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new MyViewImage().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnbrows;
    private javax.swing.JButton btnpreview;
    private javax.swing.JButton btnsave;
    private javax.swing.JLabel imagepreview;
    private javax.swing.JLabel imagesaved;
    // End of variables declaration//GEN-END:variables
}
